package com.example.juyixia;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.example.adapter.PendActiveAdapter;
import com.example.database.DbManager;
import com.exanple.data.Activeinfo;
import com.exanple.data.Messageinfo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class PendActiveActivity extends Activity implements OnClickListener {
	private ImageView mIv_Back;
	 private DbManager mgr;
	 private ListView pendactivelist;
	 private PendActiveAdapter pdAdapter;
	 private SQLiteDatabase db; 
	private List<Activeinfo> pendactive=new ArrayList<Activeinfo>();
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_pendactive);  
        db=openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
        pendactivelist=(ListView) findViewById(R.id.pendactivelist);     
        Cursor c = db.rawQuery("SELECT * FROM activeinfo ", null);
        while (c.moveToNext()) {  
        	Activeinfo active = new Activeinfo(c.getString(c.getColumnIndex("activename")),c.getString(c.getColumnIndex("parter")),c.getString(c.getColumnIndex("activetime")),c.getString(c.getColumnIndex("leader")),c.getString(c.getColumnIndex("allip")),c.getString(c.getColumnIndex("currentname")),c.getString(c.getColumnIndex("location")),c.getString(c.getColumnIndex("leaderip")));  
        	pendactive.add(active);  
        }  
        c.close();
        pdAdapter=new PendActiveAdapter(this,pendactive);
        pendactivelist.setAdapter(pdAdapter);
        pendactivelist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				final Activeinfo activeinfo=(Activeinfo)pendactivelist.getItemAtPosition(position);
				new AlertDialog.Builder(getApplicationContext(), position).setTitle("我的提示").setMessage("确定要删除吗？")                  
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                         
                    }  
                }).show();  
				new  AlertDialog.Builder(getApplicationContext())    
			   
				                .setTitle("提示：" )  
				  
				                .setMessage("同意参与？" )  
				   
				                .setPositiveButton("确定" ,  new DialogInterface.OnClickListener() {  
				                    @Override  
				                    public void onClick(DialogInterface dialog, int which) {  
				                    	new Thread() {
				       					 public void run() {
				       					 try {
				       					
				       					 Socket socket = null;
				       					
				       					 // 创建Socket
				       					 socket = new Socket(activeinfo.getLeaderip(), 22222); // IP一定是本地机器的ip
				       					 Map<String, String> replymap=new HashMap<String, String>();
				       					 replymap.put("用户名", activeinfo.getCurrentname());
				       					 replymap.put("坐标", activeinfo.getLocation());
				       					 replymap.put("活动名",activeinfo.getActivename() );
				       					 replymap.put("参与者", activeinfo.getParter());
				       					 replymap.put("allip", activeinfo.getAllip());
				       					 JSONObject replyjson=new JSONObject(replymap);
				       					 // 向服务器发送消息
				       					 PrintWriter out = new PrintWriter(new BufferedWriter(
				       					 new OutputStreamWriter(socket.getOutputStream(), "utf-8")), true);
				       					 out.println(replyjson.toString());
				       					
				       					 out.close(); 
				       				                	 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
				       				 					String str = reader.readLine();
				       				 					JSONObject rjson=new JSONObject(str);
				       				 						Activeinfo.activelist.add(new Activeinfo(rjson.getString("活动名：") , rjson.getString("参与者："), "2017.6.9"));
				       				 					
				       					 // 关闭Socket
				       					 socket.close();
				       					 } catch (Exception e) {
				       					 // TODO: handle exception
				       					
				       					 }
				       					
				       					 handler.sendEmptyMessage(0);// 执行完毕后给handler发送一个空消息
				       					 }
				       					
				       					 // 定义Handler对象
				       					 private Handler handler = new Handler() {
				       					 @Override
				       					 // 当有消息发送出来的时候就执行Handler的这个方法
				       					 public void handleMessage(Message msg) {
				       					 super.handleMessage(msg);
				       					 // 处理UI
				       					
				       					 }
				       					 };
				       					 }.start();

				                    }  
				                } ).setNegativeButton("拒绝",new DialogInterface.OnClickListener() {  
				                    @Override  
				                    public void onClick(DialogInterface dialog, int which) {  
				                         
				                    }  
				                }  )  
				   
				                .show(); 
			}
		});
        initViews();
	}
	private void initViews() {
		// TODO Auto-generated method stub
		mIv_Back = (ImageView) findViewById(R.id.iv_back);
		mIv_Back.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;

		default:
			break;
		}
	}


	}


	

